Basically to programme the AVR microcontroller there are three stages.
You can choose different methods to program your board
1. C using Arduino IDE libraries.
Program written using Arduino IDE libraries takes most of the space of microcontroller because it loads many libraries of arduino into the chip. But this is the easiest to use. For writing code install Arduino IDE. Arduino 1.8.1 is the open source Arduino software (IDE) which makes easy to write code and upload it to the board. It runs on Windows, Mac OS X and Linux. The enviroment is written in Java and based on Processing and other open source software. This software can be used with any Arduino board.
2. C using avr-gcc libraries.
From the C-source code the avr GCC compiler (together with the Preprocessor directives and the linker) produces machine code for the AVR microcontroller. Usually this code loes in the Intel Hex format pronounced (“Hexfile”). Download avr GCC compiler and avr libc. This is available free for almost all platforms and operating systems. For Ms-Windows in the package WinAVR; for Unix/Linux AVR-GCC.
3. Assembly Language.
Assembly language is the alphanumeric representation of machine code. This language gives the maximum control. The space requirement for this is very less as there are no external libraries to load. The instructions used in writing programs in assembly language are not general but specific to the microcontroller. Each company provides a set of instructions for there microcontrollers.
ISP(In System Programmers)
In-system programming (ISP), also called In-Circuit Serial Programming (ICSP), is the ability of some programmable logic devices, microcontrollers, and other embedded devices to be programmed while installed in a complete system, rather than requiring the chip to be programmed prior to installing it into the system.
Usbtiny or FabISP
The FabISP is an in-system programmer for AVR microcontrollers, designed for production within a FabLab. That is, it allows you to program the microcontrollers on other boards you make, using nothing but a USB cable and 6-pin IDC to 6-pin IDC cable. It's based on the USBtiny and V-USB firmwares, which allow the ATtiny44 to perform USB communication in software. Programming can be done through avrdude.
http://fab.cba.mit.edu/content/projects/fabisp/
Arduino as ISP
The Arduino ISP is an In-System-Programmer that is used to program AVR microcontrollers. You can use the Arduino ISP to upload sketches directly on the AVR-based Arduino boards without the need of the bootloader. Otherwise you can use it to restore the bootloader.
https://www.arduino.cc/en/Tutorial/ArduinoISP
Atmel ICE as Programmer.
The Atmel-ICE Debugger. Atmel-ICE is a powerful development tool for debugging and programming ARM® Cortex®-M based Atmel® SAM and Atmel AVR® microcontrollers with On-Chip Debug capability.
http://www.atmel.com/tools/atatmel-ice.aspx
First step is to check the working of hello board build in assignment 6. I used FabISP of my instructor to check the status. I downloaded Arduino 1.8.1. Then connected FabISP to computer and FabISP to the hello board.
Follow the following steps for arduino IDE settings.
In Tools go to and pick.
Board-> ATTiny
Processor-> ATtiny 44
Programmer-> usbtinyisp
Clock -> 20Mhz External
Port-> ttyUSB0(if multiple devices are shown you have to figure out which is the port FABISP is connected to)
After settings you should burn the bootloader into the board.
suse Tools->Burn bootloader
The burn succeeded.
Programming
Pin number that arduino ide refers to is not same as attiny pin numbering so look at google for "arduino ide attiny pins". The following is the image I found.
In my hello fdti I have connected the LED to pin 7.
Below you can find the snapshot of the program to blink a LED.
In this program i will be using button to blink a LED.
LED is connected to PIN 7 and Button is connected to PIN 3
Below you can find the program
The LED was not working properly according to the instruction given. When i was taking my hand close to the board led starts blink. Then i discussed this problem with my instructor. He told me to set the pull-up resistor and the wire was “floating”. The changes made in the program to enable pullup resistor is shown below.
Now the LED gets ON only by pressing the button.
I started reading Make: AVR Programming by Elliot Williams and i really like this book in comparsion to other sources which is used.
Next step is to program the board using AVR-C. For this my code is written in gedit. Before writing the program we need to know everything for coding, compiling, and flashing your programs into the bare silicon of the AVR chips that are placed on your tables. To do so, you’re going to need some hardware (a flash programmer) and some software (a code editor, C compiler, and the program that’ll communicate with the hardware flash programmer).
I follow this link to about to learn about AVR-C.
https://www.youtube.com/watch?v=9ADxPRjZI4Q&list=PLA6BB228B08B03EDD
Toolchain
1. Write your source code in an editor.
2. Turn your source code into machine code with a compiler (and associated software tools).
3. Using uploader software on your big computer and a hardware flash programmer, send the machine code to your target AVR chip, which stores the instructions in its nonvolatile flash memory.
4. As soon as the flash programmer is done, the AVR chip resets and starts running your code.
I will be using Notepad to write my C-code.
Once you can write and edit code, you need to compile it for the AVR, turning your human-readable C code into machine code for the AVR. The compiler we’re using, avr-gcc, is the AVR-specific version of the popular open source compiler GCC .
In addition to the compiler, you’ll need a few more software tools from the avr- gcc suite to go from source code to machine code that’s ready for uploading. A script called a makefile is commonly used to automate all of the repetitive, intermediate bits of the process.
Compiling the source code files can be tiring, especially when you have to include several source files and type the compiling command every time you need to compile. Makefiles are the solution to simplify this task.
Makefiles are special format files that help build and manage the projects automatically.
The make command allows you to manage large programs or groups of programs. As you begin to write large programs, you notice that re-compiling large programs takes longer time than re-compiling short programs. Moreover, you notice that you usually only work on a small section of the program ( such as a single function ), and much of the remaining program is unchanged.
Makefile is a program building tool which runs on Unix, Linux, and their flavors. It aids in simplifying building program executables that may need various modules.
Once you’ve compiled your C code into machine code in the right format, it’s time to send the machine code over to the chip and write it into nonvolatile flash memory. (We will be using FabISP).
Below you can find the code written in C. On my board LED is connected to PIN 7.
To run this file i will be using make file. The procedure to run make file is first open terminal in the folder having your code file (blink.c) and makefile. Then run sudo make flash.
The ouput is shown below
LED started blinking and as you can see in above picture the file is taking 100 bytes.
In this program button is applied at PIN 3 and LED at PIN 7.
The output is shown below
To learn basics of assembly programming i followed the tutorials on AVR Begineers
I will make code to blink a LED in assembly language.
First i decided to install gavrasm in my laptop. But it didn't worked for my 32 bit system.
Then i installed avra assembler using following command
I would recommend to go through AVR instruction set before writing Assembly Code.
In this program the led was not blinking or we can say delay between ON and OFF is less.
The total time taken by the delay loop is calculated as follows.
By referring the data sheet the instruction clock cycle
clr: 1 cycle
adiw: 2 cycles
brne: 2 cycles when no overflow, 1 on overflow
Given that clock is running at 20MHz= 0.00000005 sec per clock cycle. Total delay is 0.0131 sec , which is difficult to sense.
I referred the following link to find the delay program http://www.bretmulvey.com/avrdelay.html
The complete program with delay of 1 sec is shown below.I had to add the following line to include the declarations file for the attiny44 from here .
Then i used avra blink.asm to generate hex file.
Next i use sudo avrdude -c usbtiny -p attiny44 -U flash:w:blink.hex:i to burn hex file on the board.
Led start blinking with a delay of 1 sec.
The programme size of the LED program using
Arduino Sketch: 952 bytes
C Program: 100 bytes
Assembly Program: 44 bytes
In system programming, an interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code the processor is executing. The processor responds by suspending its current activities, saving its state, and executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal with the event. This interruption is temporary, and, after the interrupt handler finishes, the processor resumes normal activities. There are two types of interrupts: hardware interrupts and software interrupts.
Hardware interrupts are used by devices to communicate that they require attention from the operating system. Internally, hardware interrupts are implemented using electronic alerting signals that are sent to the processor from an external device, which is either a part of the computer itself, such as a disk controller, or an external peripheral. For example, pressing a key on the keyboard or moving the mouse triggers hardware interrupts that cause the processor to read the keystroke or mouse position.
A software interrupt is caused either by an exceptional condition in the processor itself, or a special instruction in the instruction set which causes an interrupt when it is executed.
In order for the microcontroller to respond to an interrupt event the interrupt feature of the microcontroller must be enabled along with the specific interrupt. This is done by setting the Global Interrupt Enabled bit and the Interrupt Enable bit of the specific interrupt. Interrupt Flags and Enabled bits
Each interrupt is associated with two (2) bits, an Interrupt Flag Bit and an Interrupt Enabled Bit. These bits are located in the I/O registers associated with the specific interrupt:
The interrupt flag bit is set whenever the interrupt event occur, whether or not the interrupt is enabled.
The interrupt enabled bit is used to enable or disable a specific interrupt. Basically is tells the microcontroller whether or not it should respond to the interrupt if it is triggered.
In summary basically both the Interrupt Flag and the Interrupt Enabled are required for an interrupt request to be generated as shown in the figure below.
Apart from the enabled bits for the specific interrupts the global interrupt enabled bit MUST be enabled for interrupts to be activated in the microcontoller.
For the AVR 8-bits microcontroller this bit is located in the Status I/O Register (SREG). The Global Interrupt Enabled is bit 7, the I bit, in the SREG.
In order for the interrupt to fire, you must enable it in the TIMSK register. To enable both timer0 and timer1 interrupts, use the following code in main:
enable timer overflow interrupt for both Timer0 and Timer1
I followed this link to learn about Timer Interrupts.
https://www.youtube.com/watch?v=cAui6116XKc
Output
I followed the following link to learn about external interrupt.
https://www.youtube.com/watch?v=aT1tU0EnSHw
In this program i will be using external interrupt at PIN A3(PCINT3) to blink LED connected to PIN A7.
From the data sheet of ATtiny 44a , i will be using Pin Change Mask Interrupt Register 0 (PCMSK0). Since button is connected to PCINT3 pin. Set this pin to enable interrupt.
Output: LED turns ON by passing external interrupt (pressing the button). But after continously passing the interrupt LED is not responding in the defined way.
After discussing with my instructor i got to know about switch bouncing.
This datasheet covers ATtiny24 / 44 / 84. We are using the ATtiny44.
Pin Configuration of ATtiny 44a.
First read PIN description. Some of the important points are mention below.
VCC: Supply Voltage (5 V)
Port A (PA7:PA0): Port A is a 8-bit bi-directional I/O port with internal pull-up resistors (selected for each bit)
Block Diagram overview: ATtiny44 is a low-power CMOS 8-bit microcontroller based on the AVR enhanced RISC architecture. The AVR core combines a rich instruction set with 32 general purpose working registers. All 32 registers are directly connected to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in one single instruction executed in one clock cycle.
Registers
I have gone through couple of registers.DDRA is data direction register. If any bit is high in this register it means there is an input at that PIN and if low than it isused as output.
PORTA data register: If any bit is set than that pin is high otherwise it is low.